#!/usr/bin/env python

# Copyright 2008 Nokia Siemens Networks Oyj
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""get_issues.py -- Get issues from the tracker in different formats.

Usage:  get_issues.py notes <project> <version> [<label>]
   or:  get_issues.py board <project> <version>

This script gets issues of the specified `project/version` combination and
prints them out. The output can be either in format suitable to release
notes in wiki (`notes` mode) or to index cards in task board (`board` mode).
In the `notes` mode issues with a type `task` are ignored, and an optional
`label` can be given to select only certain issues.

Exammples:
  get_issues.py notes robotframework 2.5.2 >> ReleaseNotes.wiki
  get_issues.py board swinglibrary 1.2 > issues.html

For more information, use the source.
"""

import sys
import csv
from urllib2 import urlopen
from string import Template
from collections import namedtuple


Issue = namedtuple('Issue', 'id type priority summary')
URL = Template('http://code.google.com/p/${project}/issues/csv?'
               'sort=priority+type&colspec=ID%20Type%20Priority%20Summary'
               '&q=target%3A${version}&can=1')
HEADER = """<head>
<style type="text/css">
body {
  font-family: verdana;
  font-size: 14px;
}
.issue {
  float: left;
  border: 1px solid black;
  width: 14em;
  height: 9em;
  padding: 0.5em;
  margin: 0.1em;
}
.container {
  position: relative;
  width: 100%;
  height: 100%;
}
.id {
  position: absolute;
  top: 0em;
  left: 0em;
  font-size: 2.2em;
}
.type {
  position: absolute;
  top: 1.2em;
  right: 0em;
}
.priority {
  position: absolute;
  top: 0em;
  right: 0em;
}
.summary {
  position: absolute;
  top: 4em;
  left: 0em;
  right: 0em;
  font-size: 0.8em;
}
.project {
  position: absolute;
  bottom: 0em;
  left: 0em;
  right: 0em;
  font-size: 0.7em;
  text-align: center;
  color: gray;
}
</style>
<style media="print" type="text/css">
body {
  font-size: 22px;
}
.issue {
  width: 45%;
}
.pagebreak {
  page-break-after: always;
}
</style>
</head>
<body>
"""


def get_issues(project, version, label=None):
    project = project.lower()
    if project != 'robotframework':
        project = 'robotframework-%s' % project
    url = URL.substitute(locals())
    reader = csv.reader(urlopen(url))
    for row in reader:
        if reader.line_num == 1 or not row:
            continue
        if label and label not in row[4]:
            continue
        yield Issue(*row[:4])


def release_notes(project, version, label=None):
    print '|| *ID* || *Type* || *Priority* || *Summary* ||'
    count = 0
    for issue in get_issues(project, version, label):
        if issue.type == 'Task':
            continue
        print '|| Issue %s || %s || %s || %s ||' % issue
        count += 1
    print '\nAltogether %d issues.\n' % count


def task_board(project, version):
    print HEADER
    for index, issue in enumerate(get_issues(project, version)):
        if index > 0 and index % 8 == 0:
            print '<div class="pagebreak"></div>'
        print '<div class="issue"><div class="container">'
        print '  <div class="id">%s</div>' % issue.id
        print '  <div class="type">%s</div>' % issue.type
        print '  <div class="priority">%s</div>' % issue.priority
        print '  <div class="summary">%s</div>' % issue.summary
        print '  <div class="project">%s %s</div>' % (project, version)
        print '</div></div>'
    print '</body>\n</html>\n'


if __name__ == '__main__':
    programs = {'notes': release_notes, 'board': task_board}
    try:
        programs[sys.argv[1]](*sys.argv[2:])
    except (KeyError, IndexError, TypeError):
        print __doc__
